﻿<?php
class CommentMapper extends Mapper {

    protected $tableName = 'comments';
    
    public function getCommentsByArtID($artID) {
        
        $comments = array();
        $data = $this->dao->fetch($artID, 'artID');
        
        foreach ($data as $r) {
        $comments[] = new Comment($r);
        }
        
        return $comments;
    }
    
    public function addNewComment($comment, $userID) {
        $kArray = array();
        $kArray['userID'] = $userID;
        $kArray['artID'] = $comment->artID;
        $kArray['text'] = $comment->text;
        
        $this->dao->addEntry($kArray);
    }
    
    public function getCountCommentsForArticles($data) {
        $articklesID = array();
        foreach($data as $v) $articklesID[] = $v->id;
        $articklesID = array_unique($articklesID);
        
        $result = $this->dao->fetchByArray( $articklesID, 'artID', 'artID' );
        $commentCount = array();
        if(!is_array($result)) return false;
        foreach($result as $v)  $commentCount[] = $v['artID'];
        $commentCount = array_count_values($commentCount);
        
        foreach ($data as $k => $v) $data[$k]->comCount = @$commentCount[$v->id]? $commentCount[$v->id] : 0;
    }
}
?>